package com.example.api;

import com.example.pojo.TbBookBase;
import com.example.service.TbBookBaseService;
import com.example.service.TbBookService;
import com.example.pojo.TbBook;
import com.example.util.Response;
import com.example.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2022/12/14
 * Description: url:http://localhost:8080/book/books
 */
@RestController
@RequestMapping("/book")
@Api(tags = "图书售卖管理接口")
public class AdminBooksAPI {
    @Autowired
    private TbBookService tbBookService;
    @Autowired
    private TbBookBaseService tbBookBaseService;

    //查询所有书籍销售相关信息
    @GetMapping("/books")
    @ApiOperation("查询所有书籍")
    public ResponseResult<List<TbBook>> getAllBook(@RequestHeader String token) {
        List<TbBook> bookList = tbBookService.list();
//        return bookList;
        System.out.println(bookList);
        return Response.createOkResp(bookList);
    }

    @GetMapping("/booksByCategory/{cid}")
    @ApiOperation("根据书籍分类查询书籍")
    public ResponseResult<List<TbBook>> getbyCategoryID(@PathVariable("cid") Long cid,@RequestHeader String token) {
        List<TbBook> tbBook = tbBookService.selectByCategoryId(cid);
        return Response.createOkResp(tbBook);
    }

    @GetMapping("/booksByName/{bookName}")
    @ApiOperation("根据书名模糊查询书籍")
    public ResponseResult<List<TbBook>> getbyBookName(@PathVariable("bookName") String name,@RequestHeader String token) {
        List<TbBook> bookList = tbBookService.selectByName(name);
        return Response.createOkResp(bookList);
    }

    @PostMapping("/books")
    @ApiOperation("添加一本书籍")
    public ResponseResult<TbBook> addBook(@Valid TbBook tbBook, @RequestHeader String token) {
        TbBookBase tbBookBase = new TbBookBase();
        int num = tbBookService.add(tbBook);
        System.out.println(tbBook);
        tbBookBase.setBookId(tbBook.getBookId());
        tbBookBase.setBookName(tbBook.getBookName());
        tbBookBase.setSellingPrice(tbBook.getSellingPrice());
        tbBookBase.setCategoryId(tbBook.getCategoryId());
        num += tbBookBaseService.add2(tbBookBase);
        System.out.println(tbBook.getBookId());
        System.out.println(tbBookBase);
        System.out.println("影响行数:" + num);
        return Response.createOkResp("添加成功");
    }

    @PutMapping("/books")
    @ApiOperation("修改书籍销售信息")
    public ResponseResult<Map> update(@Valid TbBook tbBook,@RequestHeader String token) {
        int num = tbBookService.update(tbBook);
        System.out.println(num);
        return Response.createOkResp("修改成功");
    }

    @ApiOperation("删除一本书，包括详细信息")
    @DeleteMapping("/books/{id}")
    public ResponseResult<Map> delete(@PathVariable("id") Long id,@RequestHeader String token) {
        int num = tbBookService.delete(id);
        return Response.createOkResp("删除成功");
    }


}
